globalInit = require("./global/exports").init;
FiberRun(function() {
	//全局初始化对象
	globalInit();

	//启动应用
	var express = require('express');
	var app = express();

	//监听的端口
	app._port = 6520;

	//跨域检查
	console.log("加载跨域配置模块");
	app.use(require("./server/cross_origin"));

	//压缩
	console.log("加载GZIP模块");
	var compression = require('compression')
	app.use(compression());

	//json、urlencoded、multipart
	console.log("加载body-parser模块");
	var bodyParser = require('body-parser');
	app.use(bodyParser.json());
	app.use(bodyParser.urlencoded({
		extended: true,
		limit: "20mb"
	}));

	//form-data
	console.log("加载form-data-parser模块");
	var multipart = require('connect-multiparty');
	app.use(multipart());

	//RAW BODY
	console.log("加载raw-body模块");
	app.use(function(req, res, next) {
		var data = '';
		var is_end;
		var fiber;
		req.setEncoding('utf8');
		req.on('data', function(chunk) {
			data += chunk;
		});
		req.on('end', function() {
			is_end = true;
			fiber && fiber.run();
		});
		req.__defineGetter__("rawBody", function() {
			if (!is_end) {
				fiber = Fiber.current;
				Fiber.yield();
			}
			return data;
		});
		next();
	});

	//Cookies
	console.log("加载cookies-parser模块");
	var cookieParser = require('cookie-parser')
	app.use(cookieParser());

	//Router
	console.log("加载Router模块");
	require("./server/routerMap")(app);

	//中间件
	console.log("加载middleware模块");
	require("./server/midInit")(app);

	console.time("Web Server服务启动:" + app._port);
	var server = require('http').createServer(app).listen(app._port, function() {
		console.timeEnd("Web Server服务启动:" + app._port);
	});
	app._server = server;

	/*TEST*/
	// console.group("测试类运作");
	// me = iClass.get("User")("Gaubee");
	// me.say();
	// console.groupEnd("测试类运作");

});